package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
	"mock/grpc_test/proto"
	"net"
)

type Server struct {}

func (s *Server) SayHello(ctx context.Context, request *proto.HelloRequest) (*proto.HelloReply, error) {

	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		fmt.Println("metadata get error")
	}
	fmt.Println(md)
	if name, ok := md["name"]; ok {
		for i, v := range name {
			fmt.Printf("i = %d, v = %v", i, v)
		}
	}

	//业务逻辑
	return &proto.HelloReply{
		Message: "hello " + request.Name,
	}, nil
}

func main() {
	server := grpc.NewServer()
	proto.RegisterGreeterServer(server, &Server{})
	listen, err := net.Listen("tcp", "0.0.0.0:8888")
	if err != nil {
		panic("failed to listen :" + err.Error())
	}
	err = server.Serve(listen)
	if err != nil {
		panic("failed to start grpc:" + err.Error())
	}
}